package com.mazaiting.classfile.attr.info;

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.constant.ConstantPool;
import com.mazaiting.classfile.constant.entity.NameAndType;

/**
 * 自闭合方法属性解析
 *
 * @author mazaiting
 * @since 2021/8/13
 */
public class EnclosingMethodAttribute extends AttributeInfo {
    /**
     * 类索引 无符号 u2
     */
    private int classIndex;
    /**
     * 方法索引 无符号 u2
     */
    private int methodIndex;

    public EnclosingMethodAttribute(ConstantPool pool, int index, int length) {
        super(pool, index, length);
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.classIndex = reader.readU2ToUnsignedShort();
        this.methodIndex = reader.readU2ToUnsignedShort();
    }

    /**
     * 获取类名
     * @return 类名
     */
    public String getClassName() {
        return pool.getClassName(classIndex);
    }

    /**
     * 获取方法名和类型
     * @return 名和类型
     */
    public NameAndType getMethodNameAndType() {
        return pool.getNameAndType(methodIndex);
    }

    @Override
    public String toString() {
        return "EnclosingMethodAttribute{" +
                "nameIndex=" + nameIndex +
                ", length=" + length +
                ", classIndex=" + classIndex +
                ", methodIndex=" + methodIndex +
                '}';
    }
}


















